using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using MySql.Data.MySqlClient;
namespace Staffing
{
	public sealed class Connections : IDisposable
	{	
		#region CTOR
		private static readonly System.Lazy<Connections> lazy =
			new System.Lazy<Connections>(() => new Connections());
		private static object synclock = new object();

		public static Connections connections { get { return lazy.Value; } }

		public Connections ()
		{
			#if!DEBUG
				Stream s = File.OpenRead(Properties.Settings.Default.CONFIGPATH);
			#else
			Stream s = File.OpenRead (Properties.Settings.Default.CONFIGPATHTEST);
			#endif
			BinaryFormatter b = new BinaryFormatter ();
			ConfigLibrary.Config config = (ConfigLibrary.Config)b.Deserialize (s);
			s.Close ();

			config.GetSqlInfo (out SQLhost, out SQLusername, out SQLpass, out DefaultDatabase, out Sqlport);
		}
		#endregion
		#region Public
		/// <summary>
		/// Gets the default database.
		/// </summary>
		/// <value>
		/// The default database.
		/// </value>
		public string DefaultDatabase {get; private set;}
		/// <summary>
		/// Gets the <see cref="MySql.Data.MySqlClient.MySqlConnection"/> class.
		/// </summary>
		/// <value>
		/// The <see cref="MySql.Data.MySqlClient.MySqlConnection"/> class.
		/// </value>
		public MySqlConnection SqlConnection {get; private set;}
		public void Dispose() { }
		/// <summary>
		/// Gets the Data as a <see cref="System.Data.DataSet"/>
		/// </summary>
		/// <returns>
		/// The data.
		/// </returns>
		/// <param name='SqlCommand'>
		/// Sql command.
		/// </param>
		public System.Data.DataSet GetData (string SqlCommand)
		{
			MySqlHelper.ExecuteDataset (SqlConnection, SqlCommand);
		}
		/// <summary>
		/// Updates the data.
		/// </summary>
		/// <param name='Database'>
		/// Database.
		/// </param>
		/// <param name='SqlCommand'>
		/// Command should be the same command used to get data <seealso cref="Connections.GetData"/>
		/// </param>
		/// <param name='Data'>
		/// Data.
		/// </param>
		/// <param name='TableName'>
		/// Table name.
		/// </param>
		public void UpdateData (string Database, string SqlCommand, System.Data.DataSet Data, string TableName)
		{
			MySqlHelper.UpdateDataSet ("Database=" + Database + ";Data Source=" + SQLhost + ";User Id=" + SQLusername + ";Password=" + SQLpass,
			                           SqlCommand, Data, TableName)
		}
		#endregion
		#region Private
		private string SQLusername;
		private string SQLhost;
		private string SQLpass;
		private int Sqlport;
		#endregion
	}
}

